<?php
/**
 * Created by PhpStorm.
 * User: ari
 * Date: 1/1/2015
 * Time: 4:37 PM
 */
namespace BC\Games\Holdem;

use BC\SiteIndex;
use BC\User\Session\UserSession;
use CPath\Build\IBuildable;
use CPath\Build\IBuildRequest;
use CPath\Render\HTML\Element\Form\HTMLButton;
use CPath\Render\HTML\Element\Form\HTMLForm;
use CPath\Render\HTML\Element\HTMLElement;
use CPath\Render\HTML\Header\HTMLMetaTag;
use CPath\Request\Executable\IExecutable;
use CPath\Request\Form\IFormRequest;
use CPath\Request\IRequest;
use CPath\Request\Session\ISessionRequest;
use CPath\Response\Common\RedirectResponse;
use CPath\Response\IResponse;
use CPath\Route\IRoutable;
use CPath\Route\RouteBuilder;

class HoldemInvite implements IRoutable, IBuildable, IExecutable
{
	const PARAM_ID = 'id';
	const PARAM_PUBLIC = 'public';

	const PATH_FORMAT = 'games/holdem/invite';
	const EVENT_INVITE = self::PATH_FORMAT;


	/**
	 * Execute a command and return a response. Does not render
	 * @param IRequest $Request
	 * @throws \BC\User\Session\Exceptions\UserSessionNotFound
	 * @throws \Exception
	 * @return IResponse the execution response
	 */
	function execute(IRequest $Request) {
		$Session = $Request;
		if(!$Session instanceof ISessionRequest)
			throw new \Exception("No session");

		$Form = new HTMLForm('POST', sprintf(self::PATH_FORMAT, ''),
			new HTMLMetaTag(HTMLMetaTag::META_TITLE, 'Texas Holdem Search'),

			new HTMLElement('fieldset', 'fieldset-invite',
				new HTMLElement('legend', 'legend-invite', 'Invite players to a table'),

//				"Public holdem table?",
//				$this->mInput['field-invite-public'] = new HTMLSelectField(self::PARAM_PUBLIC, array(
//					'Public (Anyone can join)'  => 'public',
//					'Private (Requires invite)' => 'private',
//				)),
				new HTMLButton(null, "Invite", null, null, 'field-invite')
			)
		);

		if(!$Request instanceof IFormRequest)
			return $Form;

		$UserSession = UserSession::fromSession($Session);

		$tableID = null;
		$UserSession->readSessionLog(function($cmd, $line) use (&$tableID) {
			switch($cmd) {
				case HoldemInvite::EVENT_INVITE:
					list(, $tableID) = explode(' ', $line);
					return true;
			}
			return false;
		});

		if($tableID) {
			$Table = new HoldemTable($Request, $tableID);
			return new RedirectResponse($Table->getRequestPath(), "Redirecting to Invite Table: " . $tableID, 5);

		} else {
			$Table = HoldemTable::create($Request, $UserSession->getGrantFingerprint(), null, 'inv-');

			$UserSession->writeEventToLog(HoldemInvite::EVENT_INVITE, $Table->getTableID());
			return new RedirectResponse($Table->getRequestPath(), "Created invite table. Redirecting to table: " . $Table->getTableID(), 5);
		}
	}

	// Static

	/**
	 * Handle this request and render any content
	 * @param IBuildRequest $Request the build request inst for this build session
	 * @return void
	 * @build --disable 0
	 * Note: Use doctag 'build' with '--disable 1' to have this IBuildable class skipped during a build
	 */
	static function handleBuildStatic(IBuildRequest $Request) {
		$Routes = new RouteBuilder($Request, new SiteIndex());
		$Routes->writeRoute('ANY /' . self::PATH_FORMAT, __CLASS__);
	}

	/**
	 * Route the request to this class object and return the object
	 * @param IRequest $Request the IRequest inst for this render
	 * @param array|null $Previous all previous response object that were passed from a handler, if any
	 * @param null|mixed $_arg [varargs] passed by route map
	 * @return void|bool|Object returns a response object
	 * If nothing is returned (or bool[true]), it is assumed that rendering has occurred and the request ends
	 * If false is returned, this static handler will be called again if another handler returns an object
	 * If an object is returned, it is passed along to the next handler
	 */
	static function routeRequestStatic(IRequest $Request, Array &$Previous = array(), $_arg = null) {
		return new HoldemInvite();
	}
}